package com.dswz.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.LogManager;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.dswz.dao.UserOrderDao;
import com.dswz.po.UserOrder;
import com.dswz.po.UserOrderQuery;
import com.dswz.service.UserOrderService;
import com.dswz.utils.DateUtil;
import com.dswz.utils.PageBean;

@Service("userOrderService")
@Transactional
public class UserOrderServiceImpl implements UserOrderService {
	private Logger logger = LogManager.getLogger(UserOrderServiceImpl.class);
	@Autowired
	private UserOrderDao userOrderDao;
	

	@Transactional(readOnly = true)
	public PageBean getUserOrderList(String username, String userId, String userName, String userMobile,
			String userOrderId, Byte userOrderState, String userOrderCreateTime, String userOrderFinishTime, Byte isDel,
			Integer page, Integer size) throws Exception {
		int count = 0;
		List<UserOrder> userOrderList = new ArrayList();
		UserOrderQuery userOrderQuery = new UserOrderQuery();

		userOrderQuery.setPageNo(page);

		userOrderQuery.setPageSize(size);

		userOrderQuery.setOrderByClause("user_order_create_time desc");
		if ("admin".equals(username)) {
			UserOrderQuery.Criteria createCriteria = userOrderQuery.createCriteria();

			if (StringUtils.isNotEmpty(userId)) {
				createCriteria.andUserIdEqualTo(userId);
			}
			if (StringUtils.isNotEmpty(userName)) {
				createCriteria.andUserNameEqualTo(userName);
			}
			if (StringUtils.isNotEmpty(userMobile)) {
				createCriteria.andUserMobileEqualTo(userMobile);
			}
			if (StringUtils.isNotEmpty(userOrderId)) {
				createCriteria.andUserOrderIdEqualTo(userOrderId);
			}
			if (userOrderState != null) {
				createCriteria.andUserOrderStateEqualTo(userOrderState);
			}
			if (StringUtils.isNotEmpty(userOrderCreateTime)) {
				String[] split = userOrderCreateTime.split(",");
				if (split.length == 2) {
					Date startP = DateUtil.parseDate(split[0]);

					Date endP = DateUtil.addDate(DateUtil.parseSqlDate(split[1]), 1);
					createCriteria.andUserOrderCreateTimeBetween(startP, endP);
				} else {
					Date startP = DateUtil.parseDate(split[0]);
					createCriteria.andUserOrderCreateTimeGreaterThanOrEqualTo(startP);
				}
			}
			if (StringUtils.isNotEmpty(userOrderFinishTime)) {
				String[] split = userOrderFinishTime.split(",");
				if (split.length == 2) {
					Date startP = DateUtil.parseDate(split[0]);

					Date endP = DateUtil.addDate(DateUtil.parseSqlDate(split[1]), 1);
					createCriteria.andUserOrderFinishTimeBetween(startP, endP);
				} else {
					Date startP = DateUtil.parseDate(split[0]);
					createCriteria.andUserOrderFinishTimeGreaterThanOrEqualTo(startP);
				}
			}

			if (isDel != null) {
				createCriteria.andUserOrderIsDelEqualTo(isDel);
			}
			count = this.userOrderDao.countByExample(userOrderQuery);
			userOrderList = this.userOrderDao.selectByExample(userOrderQuery);
		} else {
			UserOrderQuery.Criteria createCriteria = userOrderQuery.createCriteria();

			if (StringUtils.isNotEmpty(userId)) {
				createCriteria.andUserIdEqualTo(userId);
			}
			if (StringUtils.isNotEmpty(userOrderId)) {
				createCriteria.andUserOrderIdEqualTo(userOrderId);
			}
			if (userOrderState != null) {
				createCriteria.andUserOrderStateEqualTo(userOrderState);
			}
			if (StringUtils.isNotEmpty(userOrderCreateTime)) {
				String[] split = userOrderCreateTime.split(",");
				if (split.length == 2) {
					Date startP = DateUtil.parseDate(split[0]);

					Date endP = DateUtil.addDate(DateUtil.parseSqlDate(split[1]), 1);
					createCriteria.andUserOrderCreateTimeBetween(startP, endP);
				} else {
					Date startP = DateUtil.parseDate(split[0]);
					createCriteria.andUserOrderCreateTimeGreaterThanOrEqualTo(startP);
				}
			}
			if (StringUtils.isNotEmpty(userOrderFinishTime)) {
				String[] split = userOrderFinishTime.split(",");
				if (split.length == 2) {
					Date startP = DateUtil.parseDate(split[0]);

					Date endP = DateUtil.addDate(DateUtil.parseSqlDate(split[1]), 1);
					createCriteria.andUserOrderFinishTimeBetween(startP, endP);
				} else {
					Date startP = DateUtil.parseDate(split[0]);
					createCriteria.andUserOrderFinishTimeGreaterThanOrEqualTo(startP);
				}
			}

			if (isDel != null) {
				createCriteria.andUserOrderIsDelEqualTo(isDel);
			}
			count = this.userOrderDao.countByExample(userOrderQuery);
			userOrderList = this.userOrderDao.selectByExample(userOrderQuery);
		}
		PageBean pb = new PageBean(size.intValue(), count, page.intValue() + 1);

		pb.setContentList(userOrderList);
		return pb;
	}

	public void insertUserOrder(UserOrder userOrder) throws Exception {
		if (userOrder.getUserOrderOrderNo() == null) {
			userOrder.setUserOrderOrderNo(Integer.valueOf(0));
		}
		userOrder.setUserOrderIsDel(Byte.valueOf((byte) 0));

		userOrder.setUserOrderCreateTime(new Date());
		this.userOrderDao.insertSelective(userOrder);
	}

	public void deleteUserOrders(List<String> userOrderIds) throws Exception {
		UserOrder userOrder = new UserOrder();
		userOrder.setUserOrderIsDel(Byte.valueOf((byte) 1));
		if ((userOrderIds != null) && (userOrderIds.size() > 0)) {
			for (String userOrderId : userOrderIds) {
				userOrder.setUserOrderId(userOrderId);
				this.userOrderDao.updateByPrimaryKeySelective(userOrder);
			}
		}
	}

	public void deleteUserOrder(String userOrderId) throws Exception {
		UserOrder userOrder = new UserOrder();
		userOrder.setUserOrderIsDel(Byte.valueOf((byte) 1));
		userOrder.setUserOrderId(userOrderId);
		this.userOrderDao.updateByPrimaryKeySelective(userOrder);
	}

	public void updateUserOrder(UserOrder userOrder) throws Exception {
		this.userOrderDao.updateByPrimaryKeySelective(userOrder);
	}

	@Transactional(readOnly = true)
	public UserOrder getUserOrderById(String id) throws Exception {
		return this.userOrderDao.selectByPrimaryKey(id);
	}

	@Transactional(readOnly = true)
	public UserOrder getUserOrder(String userOrderId, Byte isDel) throws Exception {
		UserOrderQuery userOrderQuery = new UserOrderQuery();

		UserOrderQuery.Criteria createCriteria = userOrderQuery.createCriteria();
		createCriteria.andUserOrderIdEqualTo(userOrderId);

		if (isDel != null) {
			createCriteria.andUserOrderIsDelEqualTo(isDel);
		}
		List<UserOrder> userOrderList = this.userOrderDao.selectByExample(userOrderQuery);
		if ((userOrderList != null) && (userOrderList.size() > 0)) {
			return (UserOrder) userOrderList.get(0);
		}
		return null;
	}
}
